package com.lilihub.miaosha.validator;

import com.lilihub.miaosha.util.ValidateUtil;
import org.springframework.util.StringUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * @BelongsProject: miaosha2
 * @BelongsPackage: com.lilihub.miaosha.validator
 * @Author: LiYing
 * @CreateTime: 2021-04-26 13:54
 * @Description:
 */
public class isPhoneValidator implements ConstraintValidator<isPhone,String> {

    private boolean required = false;

    @Override
    public boolean isValid(String value, ConstraintValidatorContext constraintValidatorContext) {

        if (required){

            return ValidateUtil.isPhone(value);
        }else{
            if (StringUtils.isEmpty(value)){
                return true;
            }else{
                return ValidateUtil.isPhone(value);

            }
        }
    }

    @Override
    public void initialize(isPhone constraintAnnotation) {
        required = constraintAnnotation.required();
    }
}
